package com.coocaa.ops.admin.v1.panel.convert;

import com.coocaa.ops.admin.base.validator.InputParamsValidator;
import com.coocaa.ops.admin.v1.panel.validator.PanelValidator;
import com.coocaa.ops.common.core.base.enums.ResourceTypeEnum;
import com.coocaa.ops.common.core.biz.dmp.entity.DmpRegistryRlsInfoEntity;
import com.coocaa.ops.common.core.biz.panel.constant.SubPanelVersionConstants;
import com.coocaa.ops.common.core.biz.panel.entity.*;
import com.coocaa.ops.common.tool.base.InstanceUtil;
import lombok.experimental.UtilityClass;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.util.Assert;

import java.util.List;

/**
 * @author bijiahao
 * @description
 * @date 2020/6/16 18:22
 */
@UtilityClass
public class GroupPanelSaveConverter {
    /**
     * 获取需要保存的推荐位信息
     * @param entity
     * @return
     */
    public  static  List<BlockContentInfoEntity> getFinalBlockList(PanelInfoEntity entity){
        List<BlockContentInfoEntity> result = InstanceUtil.newArrayList();
        List<SubPanelConfigInfo> subPanelConfigInfoList = entity.getSubPanelList();
        for (SubPanelConfigInfo subPanelConfigInfo : subPanelConfigInfoList){
            List<VirtuallyBlockInfo> blockList = subPanelConfigInfo.getBlockList();
            List<BlockContentInfoEntity> blockContentInfoEntityList = PanelCommonSaveConverter.getFinalBlockList(entity.getStatus(), blockList);
            if (CollectionUtils.isNotEmpty(blockContentInfoEntityList)){
                result.addAll(blockContentInfoEntityList);
            }
        }
        return result;
    }

    /**
     * 获取需要保存的子版块信息
     * @param entity
     * @return
     */
    public static List<SubPanelInfoEntity> getFinalSubPanelList(PanelInfoEntity entity){
        List<SubPanelInfoEntity>  result = InstanceUtil.newArrayList();
        List<SubPanelConfigInfo> subPanelConfigInfoList = entity.getSubPanelList();
        for (SubPanelConfigInfo subPanelConfigInfo : subPanelConfigInfoList){
              subPanelConfigInfo.setHasDmp(PanelValidator.isPanelHasDmp(subPanelConfigInfo.getBlockList()) ? 1: 0);
              result.add(subPanelConfigInfo);
        }
        return result;
    }

    /**
     * 获取需要保存的板块子版块关联信息
     * @param entity
     * @return
     */
    public static List<PanelSubPanelRlsEntity> getFinalPanelSubPanelList(PanelInfoEntity entity){
        List<PanelSubPanelRlsEntity> result = InstanceUtil.newArrayList();
        List<SubPanelConfigInfo> subPanelConfigInfoList = entity.getSubPanelList();
        int seq = 1;
        for (SubPanelConfigInfo subPanelConfigInfo : subPanelConfigInfoList){
            PanelSubPanelRlsEntity panelSubPanelRlsEntity = getPanelSubPanelRlsEntity(entity, seq, subPanelConfigInfo);
            result.add(panelSubPanelRlsEntity);
            seq++;
        }
        return result;
    }

    /**
     * 获取最终保存的dmp信息
     * @param entity
     * @return
     */
    public static List<DmpRegistryRlsInfoEntity> getFinalDmpList(PanelInfoEntity entity){
        List<DmpRegistryRlsInfoEntity>  result = InstanceUtil.newArrayList();
        List<SubPanelConfigInfo> subPanelConfigInfoList = entity.getSubPanelList();
        for (SubPanelConfigInfo subPanelConfigInfo : subPanelConfigInfoList){
               handleWrapperDmpRegistryRlsInfoEntityForEachSubPanel(subPanelConfigInfo, result);
        }
        return result;
    }

    /**
     * 获取最终的板块推荐位关联信息
     * @param entity
     * @return
     */
    public static List<PanelBlockRlsEntity> getFinalPanelBlockList(PanelInfoEntity entity){
        List<PanelBlockRlsEntity> result = InstanceUtil.newArrayList();
        List<SubPanelConfigInfo> subPanelConfigInfoList = entity.getSubPanelList();
        for (SubPanelConfigInfo subPanelConfigInfo : subPanelConfigInfoList){
               handleWrapperPanelBlockRlsEntityForEachSubPanel(subPanelConfigInfo, result);
        }
        return result;
    }

    /**
     * 封装板块子版块关联信息
     * @param entity
     * @param seq
     * @param subPanelConfigInfo
     * @return
     */
    private static PanelSubPanelRlsEntity getPanelSubPanelRlsEntity(PanelInfoEntity entity, int seq, SubPanelConfigInfo subPanelConfigInfo){
        Assert.notNull(subPanelConfigInfo.getId(), "子版块ID不能为空");
        PanelSubPanelRlsEntity saveEntity = new PanelSubPanelRlsEntity();
        saveEntity.setSubPanelId(subPanelConfigInfo.getId());
        saveEntity.setPanelId(entity.getId());
        saveEntity.setPanelVersion(entity.getCurrentVersion());
        saveEntity.setPanelIsFocus(subPanelConfigInfo.getPanelIsFocus());
        saveEntity.setSeq(seq);
        saveEntity.setCreator(subPanelConfigInfo.getModifier());
        saveEntity.setModifier(subPanelConfigInfo.getModifier());
        saveEntity.setFlag(1);
        return saveEntity;
    }

    /**
     * 处理板块推荐位关联信息封装-每一子版块
     * @param subPanelConfigInfo
     * @param result
     */
    private void handleWrapperPanelBlockRlsEntityForEachSubPanel(SubPanelConfigInfo subPanelConfigInfo, List<PanelBlockRlsEntity> result){
        List<VirtuallyBlockInfo> blockList = subPanelConfigInfo.getBlockList();
        int blockSeq = 1;
        for (VirtuallyBlockInfo virtuallyBlockInfo : blockList) {
            handleWrapperPanelBlockRlsEntity(virtuallyBlockInfo, blockSeq, subPanelConfigInfo, result);
            blockSeq++;
        }
    }

    /**
     * 处理板块推荐位关联信息封装-每一推荐位
     * @param virtuallyBlockInfo
     * @param blockSeq
     * @param subPanelConfigInfo
     * @param result
     */
    private static void handleWrapperPanelBlockRlsEntity(VirtuallyBlockInfo virtuallyBlockInfo,int blockSeq, SubPanelConfigInfo subPanelConfigInfo, List<PanelBlockRlsEntity> result){
        List<BlockContentConfigInfo> commonBlockList = virtuallyBlockInfo.getCommonBlockList();
        int contentSeq = 1;
        for (BlockContentConfigInfo blockContentConfigInfo : commonBlockList){
            PanelBlockRlsEntity panelBlockRlsEntity = getPanelBlockRlsEntity(subPanelConfigInfo, blockSeq, blockContentConfigInfo, contentSeq);
            result.add(panelBlockRlsEntity);
            contentSeq++;
        }
    }

    /**
     * 处理推荐位dmp信息封装-每一子版块
     * @param subPanelConfigInfo
     * @param result
     */
    private static void handleWrapperDmpRegistryRlsInfoEntityForEachSubPanel(SubPanelConfigInfo subPanelConfigInfo, List<DmpRegistryRlsInfoEntity>  result){
        List<VirtuallyBlockInfo> blockList = subPanelConfigInfo.getBlockList();
        int blockSeq = 1;
        for (VirtuallyBlockInfo virtuallyBlockInfo : blockList) {
            handleWrapperDmpRegistryRlsInfoEntity(virtuallyBlockInfo, blockSeq, subPanelConfigInfo, result);
            blockSeq++;
        }
    }

    /**
     * 处理推荐位dmp信息封装-每一推荐位
     * @param virtuallyBlockInfo
     * @param blockSeq
     * @param subPanelConfigInfo
     * @param result
     */
    private static void handleWrapperDmpRegistryRlsInfoEntity(VirtuallyBlockInfo virtuallyBlockInfo,int blockSeq, SubPanelConfigInfo subPanelConfigInfo, List<DmpRegistryRlsInfoEntity>  result){
        List<BlockContentConfigInfo> dmpBlockList = virtuallyBlockInfo.getDmpBlockList();
        if(CollectionUtils.isEmpty(dmpBlockList)){
            return;
        }
        int contentSeq = 1;
        for (BlockContentConfigInfo blockContentConfigInfo : dmpBlockList){
            if (InputParamsValidator.isDmpContentAvailable(blockContentConfigInfo.getDmpRegistryInfo())){
                DmpRegistryRlsInfoEntity dmpRegistryRlsInfoEntity = getDmpRegistryRlsInfoEntity(subPanelConfigInfo, blockSeq, blockContentConfigInfo, contentSeq)  ;
                result.add(dmpRegistryRlsInfoEntity);
                contentSeq++;
            }
        }
    }

    /**
     * 封装dmp信息-子版块推荐位的dmp信息
     * @param subPanelConfigInfo
     * @param blockSeq
     * @param blockContentConfigInfo
     * @param contentSeq
     * @return
     */
    private static DmpRegistryRlsInfoEntity getDmpRegistryRlsInfoEntity(SubPanelConfigInfo subPanelConfigInfo, int blockSeq, BlockContentConfigInfo blockContentConfigInfo, int contentSeq){
        Assert.notNull(subPanelConfigInfo.getId(), "子版块ID不能为空");
        Assert.notNull(blockContentConfigInfo.getId(), "推荐位ID不能为空");
        DmpRegistryRlsInfoEntity dmpRegistryRlsInfoEntity = blockContentConfigInfo.getDmpRegistryInfo();
        dmpRegistryRlsInfoEntity.setParentContainerId(subPanelConfigInfo.getId());
        dmpRegistryRlsInfoEntity.setParentContainerVersion(SubPanelVersionConstants.SUB_PANEL_VERSION);
        dmpRegistryRlsInfoEntity.setParentContainerType(ResourceTypeEnum.SUB_PANEL.getValue());
        dmpRegistryRlsInfoEntity.setSubContainerId(blockContentConfigInfo.getId());
        dmpRegistryRlsInfoEntity.setSubContainerType(ResourceTypeEnum.BLOCK.getValue());
        dmpRegistryRlsInfoEntity.setSubContainerSeq(blockSeq);
        dmpRegistryRlsInfoEntity.setSubContainerContentSeq(contentSeq);
        dmpRegistryRlsInfoEntity.setCreator(subPanelConfigInfo.getModifier());
        dmpRegistryRlsInfoEntity.setModifier(subPanelConfigInfo.getModifier());
        return dmpRegistryRlsInfoEntity;
    }

    /**
     * 封装板块推荐位关联信息-子版块
     * @param subPanelConfigInfo
     * @param blockSeq
     * @param blockContentConfigInfo
     * @param contentSeq
     * @return
     */
    private static PanelBlockRlsEntity getPanelBlockRlsEntity(SubPanelConfigInfo subPanelConfigInfo,int blockSeq, BlockContentConfigInfo blockContentConfigInfo, int contentSeq){
        Assert.notNull(subPanelConfigInfo.getId(), "子版块ID不能为空");
        Assert.notNull(blockContentConfigInfo.getId(), "推荐位ID不能为空");
        PanelBlockRlsEntity saveEntity = new PanelBlockRlsEntity();
        saveEntity.setPanelId(subPanelConfigInfo.getId());
        saveEntity.setPanelVersion(SubPanelVersionConstants.SUB_PANEL_VERSION);
        saveEntity.setFlagIsSubPanel(1);
        saveEntity.setBlockId(blockContentConfigInfo.getId());
        saveEntity.setBlockSeq(blockSeq);
        saveEntity.setContentSeq(contentSeq);
        saveEntity.setCreator(subPanelConfigInfo.getModifier());
        saveEntity.setModifier(subPanelConfigInfo.getModifier());
        saveEntity.setFlag(1);
        return saveEntity;
    }

}
